package webs.servlet.ServletDemoTwo;
//测试Request对象方法
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/demo5")
public class servletTest_Five extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        //演示POST请求方式.将index.jsp表单请求方式改为POST
        BufferedReader requestReader=req.getReader();
        String infor=null;
        while((infor=requestReader.readLine())!=null)
        {
            System.out.println(infor);
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        //请求方式为GET
        //* 获取请求方式：GET
        String method=req.getMethod();
        System.out.println(method);
        //* 【重要】获取虚拟目录：/Web
        String requestContextPath=req.getContextPath();
        System.out.println(requestContextPath);
        //* 获取Servlet路径：/hello5
        String requestServletPath=req.getServletPath();
        System.out.println(requestServletPath);
        //* 获取GET方式请求参数：name=zhangsan
        String requestQueryString=req.getQueryString();
        System.out.println(requestQueryString);
        //* 【重要】获取请求的URI：/Web/hello5
        String requestURI=req.getRequestURI();
        System.out.println(requestURI);
        // 【重要】获取请求的URL：http://localhost:8080/Web/hello5
        StringBuffer requestURL=req.getRequestURL();
        System.out.println(requestURL);
        //* 获取协议版本 HTTP/1.1
        String requestProtocol=req.getProtocol();
        System.out.println(requestProtocol);
        //* 获取客户机的IP地址：IPv6地址：0:0:0:0:0:0:0:1，别人访问获取IPv4地址
        String remoteAddr=req.getRemoteAddr();
        System.out.println(remoteAddr);
        Enumeration<String> requestHeaderNames=req.getHeaderNames();
        while(requestHeaderNames.hasMoreElements())
        {
            String name=requestHeaderNames.nextElement();
            String requestHeader=req.getHeader(name);
            System.out.println(name+"-----"+requestHeader);
        }
        //user-agent:浏览器信息
        String requestHeader=req.getHeader("user-agent");
        if(requestHeader.contains("Chrome"))
        {
            System.out.println("Chrome");
        }
        if(requestHeader.contains("QQBrowser"))
        {
            System.out.println("QQBrowser");
        }
        else
            System.out.println("others");
        //Referer:告诉服务器从哪来，现需要通过超链接才能得到该请求头的信息
        //打印得到：http://localhost:8080/Web/Student.jsp
        requestHeader=req.getHeader("referer");
        System.out.println(requestHeader);
        //通过Referer实现防盗链
        if(requestHeader.contains("Web"))
        {
            System.out.println("Welcome to this page!");
            /*response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("欢迎来到当前页面！");*/
        }
        else
            System.out.println("what are you thinking!");
    }
}
